/*
 * code_generator.c
 *
 *  Created on: Nov 28, 2010
 *      Author: cpy.prefers.you@gmail.com
 */

#include "code_generator.h"

int OutputToBuffer = 0;
int OutputBufferLen = 0;
int OutputQueueLen = 0;
char OutputBuffer[OUTPUT_BUFFER_SIZE][CODE_LEN];
char OutputQueue[OUTPUT_QUEUE_SIZE][CODE_LEN];
int LabelCount = 0;

/* generate a unique Label id */
char *NewLabel(int *LabelCount)
{
    char *Label;
    Label = malloc(LABEL_LEN);
    sprintf(Label, "L%d", (*LabelCount)++);
    return Label;
}

int FlushOutputBufferToQueue()
{
    int i;
    for (i = 0; i < OutputBufferLen; i++)
        strcpy(OutputQueue[OutputQueueLen++], OutputBuffer[i]);
    OutputBufferLen = 0;
    return 0;
}

int FlushOutputQueueToStream()
{
    FlushOutputBufferToQueue();    
    int i;
    for (i = 0; i < OutputQueueLen; i++)
        printf("%s\n", OutputQueue[i]);
    OutputQueueLen = 0; 
    return 0;
}
